package org.jeecg.modules.tt_question_collect.util;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.service.ISysThirdAccountService;
import org.jeecg.modules.tt_question_collect.controller.MsgPushController;
import org.jeecg.modules.tt_question_collect.entity.TtAuditRecord;
import org.jeecg.modules.tt_question_collect.entity.TtPlanConfirm;
import org.jeecg.modules.tt_question_collect.entity.TtQuestionCollect;
import org.jeecg.modules.tt_question_collect.service.ITtAuditRecordService;
import org.jeecg.modules.tt_question_collect.service.ITtPlanConfirmService;
import org.jeecg.modules.tt_question_collect.service.ITtQuestionCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 消息生成工具
 */

@Component
public class PushSysMsgUtil {

    @Autowired
    private MsgPushController msgPushController;

    @Autowired
    private ITtQuestionCollectService ttQuestionCollectService;

    @Autowired
    private ITtAuditRecordService ttAuditRecordService;

    @Autowired
    private ISysThirdAccountService sysThirdAccountService;

    @Autowired
    private ITtPlanConfirmService ttPlanConfirmService;

    /**
     * 发送消息
     *
     * @param msgType      消息类型
     * @param questionCode 问题编码
     * @return
     */
    public boolean sendMessage(String msgType, String questionCode) {
        TtQuestionCollect ttQuestionCollect = ttQuestionCollectService.getById(questionCode);
        TtPlanConfirm ttPlanConfirm = ttPlanConfirmService.getbyquestionid(questionCode);

        if("A07".equals(msgType)||"A08".equals(msgType)||"A09".equals(msgType)||"A010".equals(msgType)||"A011".equals(msgType)){
            if (null == ttPlanConfirm.getPlanname()){
                ttPlanConfirm.setPlanname("无方案");
            }

        }


        if (null == ttQuestionCollect) {
            return false;
        }
        String toUsers = "";
        String toWxUsers = "";
        String title = "";
        String msgAbstract = "";
        String content = "";
        String contentWx = "";
        //问题需求审批消息
        if ("A01".equals(msgType) || "A02".equals(msgType)) {
            // 需求提交后/审批通过后，推送到下一审批人
            List<TtAuditRecord> auditList = ttAuditRecordService.queryAuditListByQuestionCode(questionCode,"wtlx");
            for (TtAuditRecord ttAuditRecord : auditList) {
                String thirdUuId = sysThirdAccountService.queryThirdUuIdByUserId(ttAuditRecord.getAudituserid());
                toUsers += ttAuditRecord.getAudituserid() + ",";
                toWxUsers += thirdUuId + ",";
            }
            title = "问题需求流程待审批提醒";
            content = "<p>收集人：" + ttQuestionCollect.getCollectorname() + "</p><p>收集时间：" + DateUtils.getDate("yyyy-MM-dd") + "</p><p>需求摘要：" + ttQuestionCollect.getTitlesummary() + "</p>";
            contentWx = "流程待审批提醒\n\n收集人：" + ttQuestionCollect.getCollectorname() + "\n收集时间：" + DateUtils.getDate("yyyy-MM-dd") + "\n需求摘要：" + ttQuestionCollect.getTitlesummary() + "\n";
        }
        else if ("A03".equals(msgType) || "A04".equals(msgType) || "A06".equals(msgType)) {
            // 审批退回/完成后，问题处理完成后，推送到下一审批人
            TtAuditRecord auditRecord = ttAuditRecordService.queryAuditBackListByQuestionCode(questionCode,"wtlx");
            String thirdUuId = sysThirdAccountService.queryThirdUuIdByUserId(ttQuestionCollect.getCreateById());
            toUsers += ttQuestionCollect.getCreateById() + ",";
            toWxUsers += thirdUuId + ",";
            if ("A03".equals(msgType)) {
                title = "问题需求审批结果提醒";
                content = "<p>时间：" + DateUtils.getDate("yyyy-MM-dd") + "</p><p>审核结果：已退回</p><p>退回人：" + auditRecord.getAuditusername() + "</p><p>退回原因：" + auditRecord.getAuditoption() + "</p><p>需求摘要：" + ttQuestionCollect.getTitlesummary() + "</p>";
                contentWx = "审批结果提醒\n\n时间：" + DateUtils.getDate("yyyy-MM-dd") + "\n审核结果：已退回\n退回人：" + auditRecord.getAuditusername() + "\n退回原因：" + auditRecord.getAuditoption() + "\n需求摘要：" + ttQuestionCollect.getTitlesummary() + "\n";
            }
            else if ("A04".equals(msgType)) {
                title = "问题需求审批结果提醒";
                content = "<p>时间：" + DateUtils.getDate("yyyy-MM-dd") + "</p><p>审核结果：已通过</p><p>需求摘要：" + ttQuestionCollect.getTitlesummary() + "</p>";
                contentWx = "审批结果提醒\n\n时间：" + DateUtils.getDate("yyyy-MM-dd") + "\n审核结果：已通过\n需求摘要：" + ttQuestionCollect.getTitlesummary() + "\n";
            }
            else if ("A06".equals(msgType)) {
                title = "处理结果提醒";
                content = "<p>时间：" + DateUtils.getDate("yyyy-MM-dd") + "</p><p>处理状态：" + ttQuestionCollect.getDealstatus() + "</p><p>需求摘要：" + ttQuestionCollect.getTitlesummary() + "</p>";
                contentWx = "处理结果提醒\n\n时间：" + DateUtils.getDate("yyyy-MM-dd") + "\n处理状态：" + ttQuestionCollect.getDealstatus() + "\n需求摘要：" + ttQuestionCollect.getTitlesummary() + "\n";
            }

        }
        //方案审批消息
        else if ("A07".equals(msgType) || "A08".equals(msgType)) {
        	// 需求提交后/审批通过后，推送到下一审批人
            List<TtAuditRecord> auditList = ttAuditRecordService.queryAuditListByQuestionCode(questionCode,"falc");
            for (TtAuditRecord ttAuditRecord : auditList) {
                String thirdUuId = sysThirdAccountService.queryThirdUuIdByUserId(ttAuditRecord.getAudituserid());
                toUsers += ttAuditRecord.getAudituserid() + ",";
                toWxUsers += thirdUuId + ",";
            }
            title = "方案流程待审批提醒";
            content = "<p>收集人：" + ttQuestionCollect.getCollectorname() + "</p><p>收集时间：" + DateUtils.getDate("yyyy-MM-dd") + "</p><p>方案名称：" + ttPlanConfirm.getPlanname() + "</p>";
            contentWx = "方案流程待审批提醒\n\n提出人：" + ttPlanConfirm.getFzrusername() + "\n提交时间：" + DateUtils.getDate("yyyy-MM-dd") + "\n方案名称：" + ttPlanConfirm.getPlanname() + "\n";
		}
        else if ("A09".equals(msgType) || "A010".equals(msgType) || "A011".equals(msgType)) {
            // 审批退回/完成后，问题处理完成后，推送到下一审批人
            TtAuditRecord auditRecord = ttAuditRecordService.queryAuditBackListByQuestionCode(questionCode,"falc");
            String thirdUuId = sysThirdAccountService.queryThirdUuIdByUserId(ttPlanConfirm.getFzruserid());
            toUsers += ttPlanConfirm.getFzruserid() + ",";
            toWxUsers += thirdUuId + ",";
            if ("A09".equals(msgType)) {
                title = "方案审批结果提醒";
                content = "<p>时间：" + DateUtils.getDate("yyyy-MM-dd") + "</p><p>审核结果：已退回</p><p>退回人：" + auditRecord.getAuditusername() + "</p><p>退回原因：" + auditRecord.getAuditoption() + "</p><p>方案名称：" + ttPlanConfirm.getPlanname()  + "</p>";
                contentWx = "审批结果提醒\n\n时间：" + DateUtils.getDate("yyyy-MM-dd") + "\n审核结果：已退回\n退回人：" + auditRecord.getAuditusername() + "\n退回原因：" + auditRecord.getAuditoption() + "\n方案名称：" + ttPlanConfirm.getPlanname()  + "\n";
            }
            else if ("A010".equals(msgType)) {
                title = "方案审批结果提醒";
                content = "<p>时间：" + DateUtils.getDate("yyyy-MM-dd") + "</p><p>审核结果：已通过</p><p>方案名称：" + ttPlanConfirm.getPlanname() + "</p>";
                contentWx = "审批结果提醒\n\n时间：" + DateUtils.getDate("yyyy-MM-dd") + "\n审核结果：已通过\n方案名称：" + ttPlanConfirm.getPlanname()  + "\n";
            }


      	}

        else if ("A05".equals(msgType)) {
            // 审批完成后，推送到问题处理人
            return false;
        }
        else {
            return false;
        }

        for (int i = 1; i < 3; i++) {
            if (i == 1) {
                if (oConvertUtils.isNotEmpty(toUsers) && oConvertUtils.isNotEmpty(toUsers.replace("null", "").replace(",", ""))) {
                    Result<SysAnnouncement> result = msgPushController.msgPushUserId(i + "", toUsers, title, msgAbstract, content);
                    msgPushController.msgSysPush(result.getResult());
                }
            }
            else {
                if (oConvertUtils.isNotEmpty(toWxUsers) && oConvertUtils.isNotEmpty(toWxUsers.replace("null", "").replace(",", ""))) {
                    msgPushController.msgPushUserId(i + "", toWxUsers, title, msgAbstract, contentWx);
                }
            }
        }
        return true;
    }



}
